#   Trolls Outta Luckland v1.1.0
#   Copyright 2009-2010, Gummbum
#
#   This file is part of Trolls Outta Luckland.
#
#   Trolls Outta Luckland is free software: you can redistribute it
#   and/or modify it under the terms of the GNU General Public License
#   as published by the Free Software Foundation, either version 3 of
#   the License, or (at your option) any later version.
#
#   Trolls Outta Luckland is distributed in the hope that it will be
#   useful, but WITHOUT ANY WARRANTY; without even the implied warranty
#   of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#   GNU General Public License for more details.
#
#   You should have received a copy of the GNU General Public License
#   along with Trolls Outta Luckland.  If not, see
#   <http://www.gnu.org/licenses/>.

__version__ = '1.1.0'

### Python imports
import os
from pickle import Pickler, Unpickler
import sys

try:
    ### Pygame imports
    import pygame
    ### Game library imports
    import game_globals
    from game_globals import *
except ImportError, e:
    print 'ImportError: %s' % e
    print '    in file "%s"' % os.path.abspath(sys.argv[0])

class SaveScore(object):
    def __init__(self, name, stage, score):
        self.name = name
        self.stage = stage
        self.score = score

class HighScores(object):
    def __init__(self):
        self._saves = None
        if os.access(SAVE_FILE, os.F_OK):
            if not os.access(SAVE_FILE, os.R_OK|os.W_OK):
                print 'Cannot read|write save file:', SAVE_FILE
                sys.exit()
        else:
            fh = open(SAVE_FILE, 'wb')
            fh.close()
        self._load()
    def get(self, name):
        if name not in self._saves:
            return None
        else:
            o = self._saves[name]
            return SaveScore(o.name, o.stage, o.score)
    def put(self, save_score):
        o = save_score
        if o.score > 0:
            if o.name not in self._saves:
                self._saves[o.name] = SaveScore(o.name, o.stage, o.score)
            elif self._saves[o.name].score < o.score:
                self._saves[save_score.name] = SaveScore(o.name, o.stage, o.score)
        self._save()
    def values(self):
        return list(self._saves.values())
    def _load(self):
        try:
            fh = open(SAVE_FILE, 'rb')
            unpickler = Unpickler(fh)
            self._saves = unpickler.load()
        except EOFError:
            self._saves = {
                'MAJGUMM' : SaveScore('MajGumm', 20, 200000),
                'CAPTGUMM' : SaveScore('CaptGumm', 10, 100000),
                'COLGUMM' : SaveScore('ColGumm', 5, 50000),
                'CANT' : SaveScore('Cant', 1, 700),
                'BEAT' : SaveScore('Beat', 1, 600),
                'DA' : SaveScore('Da', 1, 500),
                'GUMM' : SaveScore('Gumm', 1, 400),
            }
    def _save(self):
        fh = open(SAVE_FILE, 'wb')
        pickler = Pickler(fh)
        pickler.dump(self._saves)
